﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Indexableinsertstoolforlongitudinalandtransversalturning
    {
        public static void _Indexableinsertstoolforlongitudinalandtransversalturning(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 3; //放大系数


            double h = 12;
            double b = 12;
            double dconms = 12;
            double l3 = 9.52;
            double wta = 16;
            double wf = 22;
            double lf = 80;
            double lh = 22.25;
            double s = 2;
            double kapr = 60;
            double lfa = l3 * Math.Cos(kapr * pi / 180);
            double oal = lf + lfa;
            double wfa = l3 * Math.Sin(kapr * pi / 180);

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 6, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 3, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 2);
            Vector2 v8 = new Vector2(v3.X + oal, v3.Y);
            Vector2 v5 = new Vector2(v8.X - lh / 0.779, v4.Y);
            Vector2 v6 = new Vector2(v8.X - lh, v8.Y + dconms / 33.333);
            Vector2 v18 = new Vector2(v8.X - lh / 1.111, v3.Y);
            Vector2 v7 = new Vector2(v8.X - l3 / Math.Sin(45 * pi / 180) * Math.Sin(kapr * pi / 180), v8.Y);
            Vector2 v9 = new Vector2(v8.X - 0.1, v8.Y - s);
            Vector2 v10 = new Vector2(v9.X - (v8.X - v7.X) / 21.958, v9.Y);
            Vector2 v11 = new Vector2(v7.X, v10.Y);
            Vector2 v12 = new Vector2(v10.X - oal / 232.333, v10.Y - dconms / 6.819);
            Vector2 v13 = new Vector2(v12.X - oal / 35.291, v4.Y - dconms);
            Vector2 v14 = new Vector2(v6.X, v13.Y);
            Vector2 v15 = new Vector2(v3.X, v14.Y);
            Vector2 v16 = new Vector2(v8.X - (v8.X - v7.X) / 2, v8.Y);
            Vector2 v17 = new Vector2(v8.X - oal / 2.283, v3.Y);
            Vector2 v42 = new Vector2(v8.X - lfa, v8.Y);
            Vector2 v43 = new Vector2(v42.X, v11.Y);

            Vector2 v19 = new Vector2(v1.X, v1.Y - lf);
            Vector2 v20 = new Vector2(v2.X, v19.Y);
            Vector2 v21 = new Vector2(v19.X + 3, v19.Y);
            Vector2 v22 = new Vector2(v21.X, v21.Y + dconms / 2);
            Vector2 v23 = new Vector2(v13.X, v22.Y);
            Vector2 v24 = new Vector2(v10.X, v23.Y - dconms / 4.191);
            Vector2 v25 = new Vector2(v21.X + lf, v21.Y - wta);
            Vector2 v26 = fun.Polar(v25, kapr * pi / 180, l3);
            Vector2 v27 = fun.Polar(v26, (90 + kapr) * pi / 180, l3);
            Vector2 v28 = fun.Polar(v25, (90 + kapr) * pi / 180, l3);
            Vector2 v29 = fun.Polar(v25, (kapr + 45) * pi / 180, l3 / 3.618);
            Vector2 v30 = fun.Polar(v29, kapr * pi / 180, l3 - l3 / 3.618 * Math.Sin(45 * pi / 180) * 2);
            Vector2 v31 = fun.Polar(v30, (90 + kapr) * pi / 180, l3 - l3 / 3.618 * Math.Sin(45 * pi / 180) * 2);
            Vector2 v32 = fun.Polar(v29, (90 + kapr) * pi / 180, l3 - l3 / 3.618 * Math.Sin(45 * pi / 180) * 2);
            Vector2 v33 = fun.Polar(v25, (kapr + 45) * pi / 180, l3 / Math.Sin(45 * pi / 180) / 2);
            Vector2 v34 = new Vector2(v22.X, v22.Y - dconms);
            Vector2 v35 = fun.Polar(v28, kapr * pi / 180, l3 / 11.371);
            Vector2 v38 = new Vector2(v5.X, v21.Y);
            Vector2 v37 = fun.Polar(v38, -65 * pi / 180, lh / 7.224);
            Vector2 v36 = new Vector2(v26.X - lh, v34.Y);
            Vector2 v39 = fun.Polar(v38, 58 * pi / 180, lh / 7.159);
            Vector2 v40 = new Vector2(v38.X + lh / 1.552, v23.Y);
            Vector2 v41 = fun.Polar(v26, (90 + kapr) * pi / 180, l3 / 6.633);
            Vector2 v56 = new Vector2(v17.X, v19.Y);

            Vector2 v52 = new Vector2(v8.X + lf, v5.Y);
            Vector2 v53 = new Vector2(v52.X + oal, v52.Y);
            Vector2 v44 = new Vector2(v53.X - 0.1, v53.Y - s);
            Vector2 v45 = new Vector2(v44.X - (v8.X - v7.X) / 21.958, v44.Y);
            Vector2 v46 = new Vector2(v45.X, v52.Y - h);
            Vector2 v47 = new Vector2(v52.X, v46.Y);
            Vector2 v48 = new Vector2(v53.X - l3 / Math.Sin(45 * pi / 180) * Math.Sin(kapr * pi / 180), v53.Y);
            Vector2 v49 = new Vector2(v48.X, v45.Y);
            Vector2 v50 = new Vector2(v53.X - lh, v53.Y );
            Vector2 v51 = new Vector2(v50.X, v46.Y);
            Vector2 v54 = new Vector2(v53.X - lfa, v53.Y);
            Vector2 v55 = new Vector2(v54.X, v45.Y);
            Vector2 v57 = new Vector2(v53.X - oal / 2.283, v52.Y);
            Vector2 v58 = new Vector2(v53.X - (v53.X - v48.X) / 2, v53.Y);

            Vector2 v59 = new Vector2(v52.X, v52.Y - lf);
            Vector2 v60 = new Vector2(v53.X - oal / 24.856, v59.Y);
            Vector2 v61 = new Vector2(v46.X, v24.Y);
            Vector2 v62 = new Vector2(v59.X + lf, v60.Y - wf);
            Vector2 v63 = fun.Polar(v62, kapr * pi / 180, l3);
            Vector2 v64 = fun.Polar(v63, (90 + kapr) * pi / 180, l3);
            Vector2 v65 = fun.Polar(v62, (90 + kapr) * pi / 180, l3);
            Vector2 v66= fun.Polar(v65, kapr * pi / 180, l3 / 11.371);
            Vector2 v67 = new Vector2(v63.X - lh, v59.Y - b);
            Vector2 v68 = new Vector2(v59.X, v67.Y);
            Vector2 v69 = new Vector2(v61.X, v63.Y + (v63.X - v61.X) * Math.Tan((90 - kapr) * pi / 180));
            Vector2 v70 = fun.Polar(v62, (kapr + 45) * pi / 180, l3 / 3.618);
            Vector2 v71 = fun.Polar(v70, kapr * pi / 180, l3 - l3 / 3.618 * Math.Sin(45 * pi / 180) * 2);
            Vector2 v72 = fun.Polar(v71, (90 + kapr) * pi / 180, l3 - l3 / 3.618 * Math.Sin(45 * pi / 180) * 2);
            Vector2 v73 = fun.Polar(v70, (90 + kapr) * pi / 180, l3 - l3 / 3.618 * Math.Sin(45 * pi / 180) * 2);
            Vector2 v74 = fun.Polar(v62, (kapr + 45) * pi / 180, l3 / Math.Sin(45 * pi / 180) / 2);
            Vector2 v75 = new Vector2(v61.X, v60.Y - b / 2);
            Vector2 v76 = new Vector2(v57.X, v75.Y);






            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v15));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v18));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, line2, 0.5, contour);

            Arc c1 = fun.DrawArc3(v5, v6, v18);
            fun.drawBArc(dxf, c1, 0.5, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            fun.PolyFill(dxf, line3, 0.5, contour);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v42));
            line9.Vertexes.Add(new LwPolylineVertex(v43));
            line9.Vertexes[0].Bulge = Math.Tan(33 * pi / 180 / 4);
            fun.PolyFill(dxf, line9, 0.5, contour);

            //2
            Line line4 = new Line(v19, v20);
            line4.Layer = centre;
            line4.Linetype = Linetype.DashDot;
            line4.LinetypeScale = 3;
            dxf.AddEntity(line4);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v41));
            line5.Vertexes.Add(new LwPolylineVertex(v24));
            line5.Vertexes.Add(new LwPolylineVertex(v23));
            line5.Vertexes.Add(new LwPolylineVertex(v22));
            line5.Vertexes.Add(new LwPolylineVertex(v34));
            line5.Vertexes.Add(new LwPolylineVertex(v36));
            fun.PolyFill(dxf, line5, 0.5, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v25));
            line6.Vertexes.Add(new LwPolylineVertex(v26));
            line6.Vertexes.Add(new LwPolylineVertex(v27));
            line6.Vertexes.Add(new LwPolylineVertex(v28));
            line6.Vertexes.Add(new LwPolylineVertex(v25));
            fun.PolyFill(dxf, line6, 0.5, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v29));
            line7.Vertexes.Add(new LwPolylineVertex(v30));
            line7.Vertexes.Add(new LwPolylineVertex(v31));
            line7.Vertexes.Add(new LwPolylineVertex(v32));
            line7.Vertexes.Add(new LwPolylineVertex(v29));
            fun.PolyFill(dxf, line7, 0.5, contour);

            fun.drawcircle(dxf, v33, l3 / 8.559, 0.5, contour);

            Arc c2 = fun.DrawArc3(v37, v36, v35);
            fun.drawBArc(dxf, c2, 0.5, contour);

            Arc c3 = fun.DrawArc3(v39, v38, v37);
            fun.drawBArc(dxf, c3, 0.5, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v40));
            line8.Vertexes.Add(new LwPolylineVertex(v39));
            line8.Vertexes[0].Bulge = Math.Tan(33 * pi / 180 / 4);
            fun.PolyFill(dxf, line8, 0.5, contour);


            //3
            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v48));
            line10.Vertexes.Add(new LwPolylineVertex(v49));
            line10.Vertexes.Add(new LwPolylineVertex(v44));
            line10.Vertexes.Add(new LwPolylineVertex(v53));
            line10.Vertexes.Add(new LwPolylineVertex(v52));
            line10.Vertexes.Add(new LwPolylineVertex(v47));
            line10.Vertexes.Add(new LwPolylineVertex(v46));
            line10.Vertexes.Add(new LwPolylineVertex(v45));
            fun.PolyFill(dxf, line10, 0.5, contour);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v51));
            line11.Vertexes.Add(new LwPolylineVertex(v50));
            line11.Vertexes.Add(new LwPolylineVertex(v54));
            line11.Vertexes.Add(new LwPolylineVertex(v55));
            fun.PolyFill(dxf, line11, 0.5, contour);

            //4
            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v69));
            line12.Vertexes.Add(new LwPolylineVertex(v61));
            line12.Vertexes.Add(new LwPolylineVertex(v60));
            line12.Vertexes.Add(new LwPolylineVertex(v59));
            line12.Vertexes.Add(new LwPolylineVertex(v68));
            line12.Vertexes.Add(new LwPolylineVertex(v67));
            line12.Vertexes.Add(new LwPolylineVertex(v66));
            fun.PolyFill(dxf, line12, 0.5, contour);

            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v62));
            line13.Vertexes.Add(new LwPolylineVertex(v63));
            line13.Vertexes.Add(new LwPolylineVertex(v64));
            line13.Vertexes.Add(new LwPolylineVertex(v65));
            line13.Vertexes.Add(new LwPolylineVertex(v62));
            fun.PolyFill(dxf, line13, 0.5, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v70));
            line14.Vertexes.Add(new LwPolylineVertex(v71));
            line14.Vertexes.Add(new LwPolylineVertex(v72));
            line14.Vertexes.Add(new LwPolylineVertex(v73));
            line14.Vertexes.Add(new LwPolylineVertex(v70));
            fun.PolyFill(dxf, line14, 0.5, contour);

            fun.drawcircle(dxf, v74, l3 / 8.559, 0.5, contour);

            //坐标系
            fun.Axis(dxf, v3, 90, 180, "Y", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v17, 90, 180, "Y", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v16, 90, 180, "Y", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v21, -90, 180, "X", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v56, -90, 180, "X", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v33, kapr, kapr +90, "X", "Y", "", 1, 2, 0);

            fun.Axis(dxf, v47, 90, 180, "Y", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v57, 90, 180, "Y", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v58, 90, 180, "Y", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v59, -90, 180, "X", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v76, -90, 180, "X", "Z", "", 1, 2, 0);

            fun.Axis(dxf, v74, kapr, kapr + 90, "X", "Y", "", 1, 2, 0);





            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;



            //1
            fun.dimrot1(dxf, v4, v8, 0, lf, 1, height, arrowsize, exline, "OAL=" + oal, "", "");

            fun.dimrot1(dxf, v17, v8, 0, lf / 2, 1, height, arrowsize, exline, "LPR=" + Math.Round((v8.X - v17.X), 2), "", "");

            //2
            fun.dimrot1(dxf, v22, v34, 90, l3, 1, height, arrowsize, exline, "DCONMS=" + dconms, "", "");

            fun.dimrot1(dxf, v25, v34, 180, lf, 1, height, arrowsize, exline, "LF=" + lf, "", "");

            fun.dimrot1(dxf, v25, v26, 180, lf - (v36.Y - v25.Y) / 2 + (v26.Y - v25.Y) / 2, 1, height, arrowsize, exline, "LFA=" + lfa, "", "");

            fun.dimrot1(dxf, v36, v26, 180, lf / 3, 1, height, arrowsize, exline, "LH=" + lh, "", "");

            fun.dimrot1(dxf, v25, v56, 180, lf / 2, 1, height, arrowsize, exline, "LTA=" + (v25.X - v36.X), "", "");

            fun.dimrot1(dxf, v25, v26, -90, l3, 1, height, arrowsize, exline, "WFA=" + Math.Round(wfa, 2), "", "");

            fun.dimrot1(dxf, v25, v20, -90, l3 * 2, 1, height, arrowsize, exline, "WTA=" + wta, "", "");

            fun.jdbz2(dxf, new Line(v25, fun.Polar(v25, 0 * pi / 180, 0.1)), new Line(v25, fun.Polar(v25, kapr * pi / 180, 0.1)), l3 * 4, height, arrowsize, exline, "kapr", "", "");

            //3
            fun.dimrot1(dxf, v52, v53, 0, lf - dconms / 2, 1, height, arrowsize, exline, "OAL=" + oal, "", "");

            fun.dimrot1(dxf, v57, v53, 0, lf / 2 - dconms / 2, 1, height, arrowsize, exline, "LPR=" + Math.Round((v53.X - v57.X), 2), "", "");

            fun.dimrot1(dxf, v52, v47, 90, l3, 1, height, arrowsize, exline, "H=" + h, "", "");

            fun.dimrot1(dxf, v53, v46, -90, l3 * 2, 1, height, arrowsize, exline, "HF=" + (v53.Y - v46.Y), "", "");

            //4
            fun.dimrot1(dxf, v59, v68, 90, l3, 1, height, arrowsize, exline, "B=" + b, "", "");

            fun.dimrot1(dxf, v60, v75, 90, oal / 2, 1, height, arrowsize, exline, "=", "", "");

            fun.dimrot1(dxf, v67, v75, 90, oal / 2 + (v75.X - v60.X) / 2 - (v75.X - v67.X) / 2, 1, height, arrowsize, exline, "=", "", "");

            fun.dimrot1(dxf, v68, v62, 180, lf, 1, height, arrowsize, exline, "LF=" + lf, "", "");

            fun.dimrot1(dxf, v63, v62, 180, lf - (v67.Y - v62.Y) / 2 + (v63.Y - v62.Y) / 2, 1, height, arrowsize, exline, "LFA=" + lfa, "", "");

            fun.dimrot1(dxf, v76, v62, 180, lf / 2, 1, height, arrowsize, exline, "LTA=" + (v62.X - v67.X), "", "");

            fun.dimrot1(dxf, v67, v63, 180, lf / 3, 1, height, arrowsize, exline, "LH=" + lh, "", "");

            fun.dimrot1(dxf, v63, v62, -90, l3, 1, height, arrowsize, exline, "WFA=" + Math.Round(wfa, 2), "", "");

            fun.dimrot1(dxf, v62, v75, -90, l3 * 2, 1, height, arrowsize, exline, "WTA=" + wta, "", "");

            fun.dimrot1(dxf, v62, v60, -90, l3 * 3, 1, height, arrowsize, exline, "WF=" + wf, "", "");

            fun.jdbz2(dxf, new Line(v62, fun.Polar(v62, 0 * pi / 180, 0.1)), new Line(v62, fun.Polar(v62, kapr * pi / 180, 0.1)), l3 * 4, height, arrowsize, exline, "kapr", "", "");





            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
